
import Vue from 'vue'
import VueRouter from 'vue-router'
import login from '../components/Login'
import Home from '../components/Home'
import Welcome from '../components/welcome'
import Users from '../components/User/users'
import Roles from '../components/User/roles'
import Right from '../components/User/rights'
import params from '../components/shop/params'
import goods from '../components/shop/goods'
import categories from '../components/shop/categories'
import orders from '../components/shop/orders'
import reports from '../components/shop/reports'
import add from '../components/shop/add'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    // 重定向
    redirect: '/login'
  },
  {
    path: '/login',
    component: login
  },
  {
    path: '/Home',
    component: Home,
    redirect: '/welcome',
    children: [
      {
        path: '/welcome',
        component: Welcome
      },
      {
        path: '/users',
        component: Users
      },
      {
        path: '/roles',
        component: Roles
      },
      {
        path: '/rights',
        component: Right
      },
      {
        path: '/params',
        component: params
      },
      {
        path: '/goods',
        component: goods
      },
      {
        path: '/categories',
        component: categories
      },
      {
        path: '/orders',
        component: orders
      },
      {
        path: '/reports',
        component: reports
      },
      {
        path: '/goods/add',
        component: add
      }
    ]
  }
]

const router = new VueRouter({
  routes
})
// 如果没登录这返回登录页面
router.beforeEach((to, from, next) => {
  if (to.path === '/login') return next()
  const tokenStr = window.sessionStorage.getItem('token')
  if (!tokenStr) return next('/login')
  next()
}

)

export default router
